// 除自身之外数组的乘积

package Leetcode;

class Solution_238 {
    public int[] productExceptSelf(int[] nums) {
        int[] answer = new int[nums.length];
        int[] left = new int[nums.length]; // left[i]表示i左侧所有数字的乘积
        int[] right = new int[nums.length];
        int prod = 1;
        for (int i = 0; i < nums.length; i++) {
            left[i] = prod;
            prod *= nums[i];
        }
        prod = 1;
        for (int i = nums.length - 1; i >= 0; i--) {
            right[i] = prod;
            prod *= nums[i];
        }
        for (int i = 0; i < nums.length; i++) {
            answer[i] = left[i] * right[i];
        }

        return answer;
    }
}